package com.gcm;


import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import com.Data.Notifications;
import com.Data.User;
import com.Enums.NotificationTypes;
import com.Managers.FragmentManagerClass;
import com.Managers.NotificationManagerClass;
import com.Managers.UserManagerClass;
import com.activities.ActivityProfileUser;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.shareup.R;


public class GCMIntentService extends IntentService {

    public static final String TYPE_ADDED_COMMENT = "COMMENT";

    public static final String TYPE_ADDED_LIKE = "LIKE";

    public static final String TYPE_FOLLOWING = "FOLLOWING";

    public static final String TYPE_REQUEST = "REQ";

    public GCMIntentService() {

        super("GCMIntentService");

    }

    private void newFollow(String trigger, int id, String url) {

        String mess = String.format(getString(R.string.notification_following), "");
        //
        Intent intent = new Intent(this, ActivityProfileUser.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Bundle extras = new Bundle();
        extras.putInt(ActivityProfileUser.KEY_ID, id);
        intent.putExtras(extras);
        new NotificationManagerClass(this).mostrarNotification(trigger, mess.trim(), intent, url, R.drawable.ic_stat_sup, true);

    }

    private void processData(String data) {

        String[] result = data.split("@");

        int IdFrom = Integer.parseInt(result[1]);
        String RealName = result[2];
        String ProfilePicture = result[3];
        int Type = Integer.parseInt(result[4]);
        String Date = result[5];

        String extras = "0";
        if (result.length - 1 > 5)
            extras = result[6];
        //
        User us = new User();
        us.setID(IdFrom);
        us.setRealName(RealName);
        us.setProfilePicture(ProfilePicture);
        //
        Notifications not = new Notifications(0);
        not.setType(Type);
        not.setDate(Date);
        not.setTo(us);
        not.setExtra(extras);
        //
        NotificationManagerClass.NOTIF_ID = us.getID();

        int myId = new UserManagerClass(this).getIdUser();
        if (myId != 0) {
            if (FragmentManagerClass.aMenu != null
                        && us.getID() != myId)
                FragmentManagerClass.aMenu.updateNotifications();

            if (not.getType() == NotificationTypes.FOLLOWING.getValue())
                newFollow(us.getRealName(), us.getID(), us.getProfilePicture());
        }

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        Bundle extras = intent.getExtras();

        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                String data = extras.getString("message");
                processData(data);

            }

        }
        WakefulBroadcastReceiver.completeWakefulIntent(intent);
    }

}
